package com.ruoyi.project.mobile.user;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.card.card.domain.MemberCard;
import com.ruoyi.project.system.card.card.service.IMemberCardService;
import com.ruoyi.project.system.card.type.domain.CardType;
import com.ruoyi.project.system.card.type.service.ICardTypeService;
import com.ruoyi.project.system.coinrecord.domain.HaoqiCoinRecord;
import com.ruoyi.project.system.coinrecord.dto.HaoqiCoinRecordDto;
import com.ruoyi.project.system.coinrecord.service.IHaoqiCoinRecordService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberStatus;
import com.ruoyi.project.system.member.service.IMemberService;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/29 21:15
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/coin")
public class ApiHaoqiCoinController extends BaseController {

	@Autowired
	private IHaoqiCoinRecordService haoqiCoinRecordService;

	@Autowired
	private IMemberService memberService;

	@Autowired
	private IMemberCardService memberCardService;

	@Autowired
	private ICardTypeService cardTypeService;


	/**
	 * 查询好奇币数量
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R haoqiCoinNum(@RequestParam String uid){
		Member member = this.memberService.selectMemberById(uid);
		if(member==null){
			return R.error("用户不存在");
		}
		return RFactory.generateR(member.getHaoqiCoin());
	}

	/**
	 * 查询支出列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/expenditure")
	public R haoqiCoinExpenditure(@RequestParam String uid){
		Member member = this.memberService.selectMemberById(uid);
		if(member==null){
			return R.error("用户不存在");
		}
		HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
		haoqiCoinRecord.setType(1);
		haoqiCoinRecord.setMemberId(uid);
		startPage();
		List<HaoqiCoinRecordDto> haoqiCoinRecords = this.haoqiCoinRecordService.selectHaoqiCoinRecordList(haoqiCoinRecord);
		return RFactory.generateR(getDataTable(haoqiCoinRecords));
	}

	/**
	 * 查询收入列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/income")
	public R haoqiCoinIncome(@RequestParam String uid){
		Member member = this.memberService.selectMemberById(uid);
		if(member==null){
			return R.error("用户不存在");
		}
		HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
		haoqiCoinRecord.setType(0);
		haoqiCoinRecord.setMemberId(uid);
		startPage();
		List<HaoqiCoinRecordDto> haoqiCoinRecords = this.haoqiCoinRecordService.selectHaoqiCoinRecordList(haoqiCoinRecord);
		return RFactory.generateR(getDataTable(haoqiCoinRecords));
	}

	/**
	 * 查询用户卡列表
	 * @param uid
	 * @param type
	 * @return
	 */
	@GetMapping("/cards")
	public R memberCardList(@RequestParam String uid,@RequestParam(required = false) Integer type){
		Member member = this.memberService.selectMemberById(uid);
		if(member==null||member.getStatus().equals(MemberStatus.DISABLE.getValue())){
			return R.error("账户异常,请联系管理员");
		}
		MemberCard memberCard = new MemberCard();
		memberCard.setMemberId(uid);
		if(type==null){
			memberCard.setType(0);
			memberCard.setIsDefault(1);
			List cardList = new ArrayList();
			List<MemberCard> cards = this.memberCardService.selectMemberCardList(memberCard);
			cardList.add(cardList.isEmpty()?null:cards.get(0));
			memberCard.setType(1);
			List<MemberCard> weixins = this.memberCardService.selectMemberCardList(memberCard);
			if(weixins.isEmpty()){
				MemberCard weixinCard = new MemberCard();
				weixinCard.setType(1);
				weixinCard.setIsDefault(1);
				weixinCard.setCardId(member.getMobile());
				weixinCard.setMemberId(uid);
				weixinCard.setCardName("微信");
				weixinCard.setLogo("/img/weixin_logo.png");
				this.memberCardService.insertMemberCard(weixinCard);
				cardList.add(weixinCard);
			}else{
				cardList.add(weixins.get(0));
			}
			memberCard.setType(2);
			List<MemberCard> alipays = this.memberCardService.selectMemberCardList(memberCard);
			if(alipays.isEmpty()){
				MemberCard alipay = new MemberCard();
				alipay.setLogo("/img/alipay_logo.png");
				alipay.setCardName("支付宝");
				cardList.add(alipay);
			}else{
				cardList.add(alipays.get(0));
			}
			return RFactory.generateR(cardList);
		}
		memberCard.setType(type);
		List<MemberCard> cardList = this.memberCardService.selectMemberCardList(memberCard);
		return RFactory.generateR(cardList);
	}

	/**
	 * 查询银行卡类型列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/cardtype")
	public R cardTypeList(@RequestParam String uid){
		List<CardType> cardTypes = this.cardTypeService.findAll();
		return RFactory.generateR(cardTypes);
	}

	/**
	 * 新增银行卡
	 * @param memberCard
	 * @return
	 */
	@PostMapping("/cards")
	public R addUserCards(@RequestBody MemberCard memberCard){
		memberCard.setIsDefault(0);
		memberCard.setType(0);
		this.memberCardService.insertMemberCard(memberCard);
		return R.ok();
	}

	/**
	 * 提现
	 * @param haoqiCoinRecord
	 * @return
	 */
	@PostMapping("/withdrawal")
	@Transactional
	public R haoqiCoinWithdrawal(@RequestBody HaoqiCoinRecord haoqiCoinRecord){
		Member member = this.memberService.selectMemberById(haoqiCoinRecord.getMemberId());
		if(member.getHaoqiCoin().compareTo(haoqiCoinRecord.getNum())==-1){
			return R.error("余额不足");
		}

		haoqiCoinRecord.setType(1);
		haoqiCoinRecord.setStatus(0);
		haoqiCoinRecord.setCreated(DateUtils.getNowDate());
		//如果提现方式是支付宝并且没有资源ID
		if(haoqiCoinRecord.getGetType()==2&&haoqiCoinRecord.getResId()==null){
			//取消原本的默认卡
			this.memberCardService.removeDefault(haoqiCoinRecord.getMemberId(),2);
			MemberCard memberCard = new MemberCard();
			memberCard.setType(2);
			memberCard.setIsDefault(1);
			memberCard.setLogo("/img/alipay_logo.png");
			memberCard.setCardName("支付宝");
			memberCard.setMemberId(haoqiCoinRecord.getMemberId());
			memberCard.setCardId(haoqiCoinRecord.getAlipayNo());
			this.memberCardService.insertMemberCard(memberCard);
			haoqiCoinRecord.setResId(memberCard.getId());
		}else if(haoqiCoinRecord.getGetType()==0){
			//取消原本的默认卡
			this.memberCardService.removeDefault(haoqiCoinRecord.getMemberId(),0);
			//设置资源为默认资源
			MemberCard update = new MemberCard();
			update.setId(haoqiCoinRecord.getResId());
			update.setIsDefault(1);
			this.memberCardService.updateMemberCard(update);
		}
		this.haoqiCoinRecordService.insertHaoqiCoinRecord(haoqiCoinRecord);
		//修改个人余额
		Member update = new Member();
		update.setId(member.getId());
		update.setHaoqiCoin(member.getHaoqiCoin().subtract(haoqiCoinRecord.getNum()));
		this.memberService.updateMember(update);
		return R.ok();
	}

}
